﻿using PRO.Entity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using PRO.Common;
using PRO.User;
using PRO.Product;
using PRO.Config;

namespace web.Member
{
    public partial class deliver_address_add : PageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            this.Title = "添加收货地址-" + BaseConfigs.Title;
            if (UserInfo.ID <= 0)
                Response.Redirect(string.Format("/login?url={0}", Request.RawUrl.UrlEncode()));
        }
        protected void btn_Save_Click(object sender, EventArgs e)
        {
            if (this.txtName.Text.IsNullOrWhiteSpace())
            {
                Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script type='text/javascript'>alert('收货人姓名不能为空！');</script>");
                return;
            }
            if (this.txtAddress.Text.IsNullOrWhiteSpace())
            {
                Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script type='text/javascript'>alert('收货地址不能为空！');</script>");
                return;
            }
            if (this.txtZip.Text.IsNullOrWhiteSpace())
            {
                Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script type='text/javascript'>alert('邮政编码不能为空！');</script>");
                return;
            }
            else
            {
                if (!this.txtZip.Text.IsNumber())
                {
                    Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script type='text/javascript'>alert('邮政编码只能是数字组成！');</script>");
                    return;
                }
            }
            if (this.txtMoble.Text.IsNullOrWhiteSpace() && this.txtZone.Text.IsNullOrWhiteSpace() && this.txtPhone.Text.IsNullOrWhiteSpace())
            {
                Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script type='text/javascript'>alert('电话和手机必须填一个！');</script>");
                return;
            }
            else
            {
                if (!this.txtMoble.Text.IsNullOrWhiteSpace())
                {
                    if (!this.txtMoble.Text.IsNumber())
                    {
                        Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script type='text/javascript'>alert('手机号只能是纯数字组成！');</script>");
                        return;
                    }

                    if (this.txtMoble.Text.Length != 11)
                    {
                        Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script type='text/javascript'>alert('手机号应该是11位！');</script>");
                        return;
                    }
                }

                if (!this.txtZone.Text.IsNullOrWhiteSpace())
                {
                    if (!this.txtZone.Text.IsNumber())
                    {
                        Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script type='text/javascript'>alert('区号只能是数字组成！');</script>");
                        return;
                    }
                }
                if (!this.txtPhone.Text.IsNullOrWhiteSpace())
                {
                    if (!this.txtPhone.Text.IsNumber())
                    {
                        Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script type='text/javascript'>alert('电话号码只能是数字组成！');</script>");
                        return;
                    }
                }
            }

            Deliver_address_Info info = new Deliver_address_Info();
            info.UserID = UserInfo.ID;
            info.Consignee = this.txtName.Text.RemoveHTML();
            info.Address = this.txtAddress.Text.RemoveHTML();
            info.Zip = this.txtZip.Text;
            info.Moble = this.txtMoble.Text;
            if (!this.txtZone.Text.IsNullOrWhiteSpace() && !this.txtPhone.Text.IsNullOrWhiteSpace())
                info.Phone = this.txtZone.Text + "-" + this.txtPhone.Text + "-" + this.txtExtension.Text;
            info.Default_Addr = this.chbDefault.Checked;
            if (DeliverLib.AddDeliver_Address(info))
            {
                Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script type='text/javascript'>alert('添加成功！');</script>");
                if (CVGRequest.GetQueryString("url").IsNullOrWhiteSpace())
                    Response.Redirect("/member/address");
                else
                    Response.Redirect(CVGRequest.GetQueryString("url"));
            }
            else
                Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script type='text/javascript'>alert('添加失败！');</script>");
        }
    }
}